﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using Portal.Systems.Model;
using Portal.Database.Utility;
using Portal.Database.Service;
using Portal.Web.Utility;
using Portal.Core;
using Portal.Systems.Bussiness.Template;

namespace Portal.Systems.Bussiness
{
    public class PageProcessBll 
    {
        public static PageCls[] ReadingPage(string SiteId, string Keyword)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TablePage where AssetCode=@AssetCode order by SortIndex",
                    new DbParam[]{
                        new DbParam("AssetCode",OSiteParam.SiteCode)
                    });
            if (dsResult.Tables[0].Rows.Count ==  0)
            {
                return new PageCls[0];
            }
            PageCls[] CmsPages = PageParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return CmsPages;
        }

        public static PageCls CreatePageModel(string SiteId, string PageId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TablePage where (PageId=@PageId or PageAlias=@PageId) ",
                    new DbParam[] 
                    { 
                        new DbParam("PageId", PageId),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            PageCls OPage = PageParser.ParseFromDataRow(dsResult.Tables[0].Rows[0]);
            PageZoneCls[] PageZones=PageZoneProcessBll.ReadingPageZone(SiteId, PageId, null);
            for (int iIndex = 0; iIndex < PageZones.Length; iIndex++)
            {
                PageZones[iIndex].PageId = PageId;
                PageZoneModuleCls[] 
                    PageZoneModules = PageZoneModuleProcessBll.ReadingPageZoneModule(SiteId, PageZones[iIndex].PageZoneId);
                for (int iIndexPageZoneModule = 0; iIndexPageZoneModule < PageZoneModules.Length; iIndexPageZoneModule++)
                {
                    PageZoneModules[iIndexPageZoneModule].PageId = PageId;
                    PageZones[iIndex].PageZoneModules.Add(PageZoneModules[iIndexPageZoneModule]);
                }
                OPage.PageZones.Add(PageZones[iIndex]);
            }

            dsResult.Clear();
            dsResult.Dispose();
            return OPage;
        }



        public static void AddPage(string SiteId, PageCls OPage)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            if (string.IsNullOrEmpty(OPage.PageId))
            {
                OPage.PageId = System.Guid.NewGuid().ToString();
            }
            DBService.Insert(null, "TablePage",
                    new DbParam[] 
                    { 
                        new DbParam("PageId", OPage.PageId),
                        new DbParam("PageAlias",OPage.PageAlias),
                        new DbParam("PageTitle",OPage.PageTitle),
                        new DbParam("PageKeyword",OPage.PageKeyword),
                        new DbParam("AssetCode",OSiteParam.SiteCode),
                        new DbParam("DataLanguage",DataLanguage),
                        new DbParam("Active",OPage.Active),
                        new DbParam("PageOwner",OPage.PageOwner),
                        new DbParam("SortIndex",OPage.SortIndex),
                    });
        }


        public static void DeletePage(string SiteId, string PageId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string Query =
                " Delete from TablePageZoneModule where frkPageId=@PageId; "+
                " Delete from TablePageZone where frkPageId=@PageId; " +
                " Delete from TablePage where PageId=@PageId ";
            DBService.ExecuteNonQuery(null, Query,
                    new DbParam[] 
                    { 
                        new DbParam("PageId", PageId),
                    });
        }


        public static void SavePage(string SiteId,string PageId, PageCls OPage)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            DBService.Update(null, "TablePage","PageId",PageId,
                    new DbParam[] 
                    { 
                        new DbParam("PageAlias",OPage.PageAlias),
                        new DbParam("PageTitle",OPage.PageTitle),
                        new DbParam("PageKeyword",OPage.PageKeyword),
                        new DbParam("Active",OPage.Active),
                        new DbParam("SortIndex",OPage.SortIndex),
                    });
        }


        public static string DuplicatePage(string SiteId, string PageId)
        {
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            string NewPageId = System.Guid.NewGuid().ToString();

            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            PageCls OPage = CreatePageModel(SiteId, PageId);
            PageZoneCls[] PageZones = PageZoneProcessBll.ReadingPageZoneWithModules(SiteId, PageId);
            IDbTransaction Trans = DBService.BeginTransaction();
            //doan nay bat dau tao
            try
            {
                
                DBService.Insert(Trans, "TablePage", new DbParam[]{
                    new DbParam("PageId",NewPageId),
                    new DbParam("PageAlias",OPage.PageAlias+"(duplicate)"),
                    new DbParam("PageTitle",OPage.PageTitle),
                    new DbParam("PageKeyword",OPage.PageKeyword),
                    new DbParam("AssetCode",OPage.AssetCode),
                    new DbParam("DataLanguage",OPage.DataLanguage),
                    new DbParam("Active",OPage.Active),
                    new DbParam("PageOwner",OPage.PageOwner),
                    new DbParam("SortIndex",OPage.SortIndex),
                });
                //doan nay nhet module vao
                for (int iIndex = 0; iIndex < PageZones.Length; iIndex++)
                {
                    string NewPageZoneId = System.Guid.NewGuid().ToString();
                    DBService.Insert(Trans, "TablePageZone", new DbParam[]
                    {
                        new DbParam("PageZoneId",NewPageZoneId),
                        new DbParam("ZoneAlias",PageZones[iIndex].ZoneAlias),
                        new DbParam("ZoneName",PageZones[iIndex].ZoneName),
                        new DbParam("AssetCode",PageZones[iIndex].AssetCode),
                        new DbParam("DataLanguage",PageZones[iIndex].DataLanguage),
                        new DbParam("frkPageId",NewPageId),
                        new DbParam("Active",PageZones[iIndex].Active),
                        new DbParam("SortIndex",PageZones[iIndex].SortIndex),
                    });

                    for (int iPageZoneModule = 0; iPageZoneModule < PageZones[iIndex].PageZoneModules.Count; iPageZoneModule++)
                    {
                        string NewPageZoneModuleId = System.Guid.NewGuid().ToString();
                        DBService.Insert(Trans, "TablePageZoneModule", new DbParam[]
                        {
                            new DbParam("PageZoneModuleId",NewPageZoneModuleId),
                            new DbParam("frkPageZoneId",NewPageZoneId),
                            new DbParam("ModuleTitle", PageZones[iIndex].PageZoneModules[iPageZoneModule].ModuleTitle),
                            new DbParam("Visible", PageZones[iIndex].PageZoneModules[iPageZoneModule].Visible),
                            new DbParam("SortIndex", PageZones[iIndex].PageZoneModules[iPageZoneModule].SortIndex),
                            new DbParam("ModuleId", PageZones[iIndex].PageZoneModules[iPageZoneModule].ModuleId),
                            new DbParam("AssetCode", PageZones[iIndex].PageZoneModules[iPageZoneModule].AssetCode),
                            new DbParam("DataLanguage", PageZones[iIndex].PageZoneModules[iPageZoneModule].DataLanguage),
                            new DbParam("frkPageId", NewPageId),
                            new DbParam("ServiceId", PageZones[iIndex].PageZoneModules[iPageZoneModule].ServiceId),
                            new DbParam("PlugInViewId", PageZones[iIndex].PageZoneModules[iPageZoneModule].PlugInViewId),
                            new DbParam("CssClass", PageZones[iIndex].PageZoneModules[iPageZoneModule].CssClass),
                            new DbParam("XmlDataConfig", PageZones[iIndex].PageZoneModules[iPageZoneModule].XmlDataConfig),
                        });

                    }
                }

                //doan nay dua module vao zone
                Trans.Commit();
            }
            catch (Exception ex)
            {
                Trans.Rollback();
                throw (ex);
            }
            return NewPageId;
        }


    }
}

